package com.evaluation.controller;

import com.evaluation.dto.TeacherDTO;
import com.evaluation.entity.*;
import com.evaluation.enums.UserTypeEnum;
import com.evaluation.mapper.CourseTeaEntityMapper;
import com.evaluation.mapper.EvaluateEntityMapper;
import com.evaluation.mapper.TeacherEntityMapper;
import com.evaluation.utils.Layui;
import com.evaluation.utils.Message;
import com.evaluation.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 教师控制器
 * @author: mty
 * @date: 2021/09/02 15:35
 * @Description:
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    TeacherEntityMapper teacherEntityMapper;
    @Autowired
    EvaluateEntityMapper evaluateEntityMapper;
    @Autowired
    CourseTeaEntityMapper courseTeaEntityMapper;

    /**
     * 添加教师信息
     * @param teacher
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public Message add(@RequestBody TeacherDTO teacher) {
         if(StringUtils.isEmpty(teacher.getTeaRealname())){
            return Message.error("请填写真实姓名");
        }
        if(StringUtils.isEmpty(teacher.getTeaAge())){
            return Message.error("请填写年龄");
        } if(StringUtils.isEmpty(teacher.getLoginName())){
            return Message.error("请填写登录名");
        } if(StringUtils.isEmpty(teacher.getLoginPw())){
            return Message.error("请填写登录密码");
        }
        if(teacher.getCourseIds().length<=0){
            return Message.error("请选择代课课程");
        }
        Integer maxId = teacherEntityMapper.findMaxId();
         if(maxId==null){
             maxId=0;
         }
        teacher.setTeaBianhao(StringUtil.gneerateSn(maxId+1));
        TeacherEntityExample teacherEntityExample = new TeacherEntityExample();
        teacherEntityExample.or().andLoginNameEqualTo(teacher.getLoginName());
        TeacherEntity teacherEntity = teacherEntityMapper.selectOneByExample(teacherEntityExample);
        if(teacherEntity!=null){
            return Message.error("登录名称已存在");
        }
        List<CourseTeaEntity> teaEntities = new ArrayList<>();
        if(teacherEntityMapper.insert(teacher)<=0){
            return Message.error("老师添加失败");
        }
        for (int i = 0; i < teacher.getCourseIds().length; i++) {
            CourseTeaEntity courseTeaEntity = new CourseTeaEntity();
            courseTeaEntity.setCourseId(teacher.getCourseIds()[i]);
            courseTeaEntity.setTeacherId(teacher.getTeaId());
            teaEntities.add(courseTeaEntity);
        }
            if( courseTeaEntityMapper.insertBatch(teaEntities)<=0){
                return Message.error("代课添加失败");
            }
        return Message.success("老师添加成功");
    }

    /**
     * 老师删除操作
     * @param entity
     * @return
     */
    @ResponseBody
    @PostMapping("/delete")
    public Message delete(@RequestBody TeacherEntity entity) {
        try{
            teacherEntityMapper.deleteByPrimaryKey(entity.getTeaId());
        }catch (Exception e){
            return Message.error("删除失败,该老师下有关联数据");
        }
        return Message.success("删除成功");
    }

    /**
     * 编辑老师信息
     * @param teacher
     * @return
     */
    @ResponseBody
    @PostMapping("/edit")
    public Message edit(@RequestBody TeacherDTO teacher) {
       if(StringUtils.isEmpty(teacher.getTeaRealname())){
            return Message.error("请填写真实姓名");
        }
        if(StringUtils.isEmpty(teacher.getTeaAge())){
            return Message.error("请填写年龄");
        } if(StringUtils.isEmpty(teacher.getLoginName())){
            return Message.error("请填写登录名");
        } if(StringUtils.isEmpty(teacher.getLoginPw())){
            return Message.error("请填写登录密码");
        }
        if(teacher.getCourseIds().length<=0){
            return Message.error("请选择代课课程");
        }
        TeacherEntityExample teacherEntityExample = new TeacherEntityExample();
        teacherEntityExample.or().andLoginNameEqualTo(teacher.getLoginName());
        TeacherEntity teacherEntity = teacherEntityMapper.selectOneByExample(teacherEntityExample);
        if(teacherEntity!=null){
            if(!teacherEntity.getTeaId().equals(teacher.getTeaId())){
                return Message.error("登录名称已存在");
            }
        }
        CourseTeaEntityExample example = new CourseTeaEntityExample();
        example.or().andTeacherIdEqualTo(teacher.getTeaId());
        //先清空后插入
        courseTeaEntityMapper.deleteByExample(example);
        List<CourseTeaEntity> teaEntities = new ArrayList<>();
        for (int i = 0; i < teacher.getCourseIds().length; i++) {
            CourseTeaEntity courseTeaEntity = new CourseTeaEntity();
            courseTeaEntity.setCourseId(teacher.getCourseIds()[i]);
            courseTeaEntity.setTeacherId(teacher.getTeaId());
            teaEntities.add(courseTeaEntity);
        }
        if( courseTeaEntityMapper.insertBatch(teaEntities)<=0){
            return Message.error("代课编辑失败");
        }
        if(teacherEntityMapper.updateByPrimaryKey(teacher)<=0){
            return Message.error("老师编辑失败");
        }
        return Message.success("老师编辑成功");
    }

    @RequestMapping("/select")
    public Layui select(@RequestParam(required = false) String teaRealname, @RequestParam(value = "page") Integer page,
                        @RequestParam(value = "limit") Integer limit) {
        TeacherEntityExample example = new TeacherEntityExample();
        if (!StringUtils.isEmpty(teaRealname)) {
            example.or().andTeaRealnameLike("%"+teaRealname+"%");
        }
        Long cou = teacherEntityMapper.countByExample(example);
        if(page!=1){
            page=(page-1)*10;
        }else{
            page=page-1;
        }
        return Layui.data(cou.intValue(), teacherEntityMapper.selectByExamplePaging(example, page, limit));
    }

    @RequestMapping("/getTeacher")
    public TeacherDTO getAdmin(@RequestParam(value = "teaId") Integer teaId) {
        TeacherEntity entity = teacherEntityMapper.selectByPrimaryKey(teaId);
        CourseTeaEntityExample example = new CourseTeaEntityExample();
        example.or().andTeacherIdEqualTo(entity.getTeaId());
        List<CourseTeaEntity> courseTeaEntities = courseTeaEntityMapper.selectByExample(example);
        Integer[] arr = new Integer[courseTeaEntities.size()];
        for (int i = 0; i < courseTeaEntities.size(); i++) {
            arr[i] = courseTeaEntities.get(i).getCourseId();
        }
        TeacherDTO dto = new TeacherDTO();
        dto.setCourseIds(arr);
        dto.setLoginName(entity.getLoginName());
        dto.setLoginPw(entity.getLoginPw());
        dto.setTeaId(entity.getTeaId());
        dto.setDel(entity.getDel());
        dto.setTeaAge(entity.getTeaAge());
        dto.setTeaBianhao(entity.getTeaBianhao());
        dto.setTeaRealname(entity.getTeaRealname());
        dto.setTeaSex(entity.getTeaSex());
        return dto;
    }

    /**
     * 若存在--提示已经评论过
     * @param teaId
     * @return
     */
    @ResponseBody
    @RequestMapping("/eva_check")
    public Message evaCheck(@RequestParam(value = "teaId") Integer teaId,@RequestParam(value = "courseId")Integer courseId,HttpServletRequest request) {
        HttpSession session = request.getSession();
        //学生id
        Integer stuId = Integer.parseInt(session.getAttribute("loginUserId").toString());
        EvaluateEntityExample example = new EvaluateEntityExample();
        example.or().andStuIdEqualTo(stuId).andTeaIdEqualTo(teaId).andCouseIdEqualTo(courseId);
        if(evaluateEntityMapper.selectByExample(example).size()>0){
            return Message.error("你已经评论过该课程该教师的指标！");
        }
        return  Message.success();
    }
}
